﻿// 药品统计

// 解析URL
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var username = decodeURIComponent(parameterList[0]);  //用户名
var hospital = decodeURIComponent(parameterList[1]);  //医院
var manufacturer = decodeURIComponent(parameterList[2]);  //生产厂商
var searchword = decodeURIComponent(parameterList[3]);  //药品名称/药品编号
var pageNo  = decodeURIComponent(parameterList[4]);  //页码
var pageSize = decodeURIComponent(parameterList[5]);  //每页显示数据数目
console.log(parameterList);
$("#hospital").val(hospital);
$("#manufacturer").val(manufacturer);
$("#search_input").val(searchword);
$("#pageSize").val(pageSize+"条/页");

if(manufacturer != ""){
    $("#manufacturer").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#manufacturer").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}

// 根据生产厂商、搜索词查询药品消耗信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectDrugConsume",
    data: {
        manufacturer: manufacturer,
        searchword: searchword
    },
    success: function(data) {
        // console.log("success");
        console.log(data);
        getDrugList(data);
        // 输出药品总数、药品总价
        $("#drug_num").html(data.total_num);
        $("#drug__money").html(data.total_price);
    },
    error: function (data) {
        // console.log("error");
        // console.log(data);
    }
});
var getDrugList = function (data) {
    drugList = data.list;
    length = drugList.length;
    start = (pageNo - 1) * pageSize;
    end = pageNo * pageSize;
    pageCount = Math.ceil(length / pageSize);
    if(end >= length){
        end = length;
    }
    // console.log("总条数："+length);
    // console.log("总页数："+pageCount);
    // console.log("当前页数："+pageNo);
    // console.log("一页数目："+pageSize);
    // console.log("显示第"+start+"条到第"+end+"条数据");
    // $("#pageCount").html("第"+pageNo+"页|共"+pageCount+"页");
    $("#pageNo").val(pageNo);
    $("#totalcount").html("当前共"+length+"条记录");
    if(pageNo == 1){
        $("#pre").removeClass("paging").addClass("notAllowed");
    }
    if(pageNo == pageCount){
        $("#next").removeClass("paging").addClass("notAllowed");
    }
    if(length == 0){
        insertEmpty();
    }
    else{
        insertHtml(start,end);
    }
};
var insertEmpty = function () {
    var strhtml = '<tr>';
    strhtml = strhtml + '<td colspan="12">暂无数据</td>';
    $("#druglist").append(strhtml);
};
var insertHtml = function (start,end) {
    for (var i = start; i < end; i++) {
        var no = parseInt(i)+1;
        var druginfo = drugList[i];
        var strhtml = '<tr id="' + i + '" arrId="' + i + '">';
        strhtml = strhtml + '<td>' + no + '</td>';
        strhtml = strhtml + '<td title=' + druginfo.drug_name + '>' + druginfo.drug_name + '</td>';
        strhtml = strhtml + '<td title=' + druginfo.drug_capacity + '>' + druginfo.drug_capacity + '</td>';
        strhtml = strhtml + '<td title=' + druginfo.drug_manufacturer + '>' + druginfo.drug_manufacturer + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_amount + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_unit + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_price + '</td>';
        strhtml = strhtml + '<td>' + druginfo.total_price + '</td>';
        $("#druglist").append(strhtml);
    }
};

// 搜索框下拉数据
$.ajax({
    type: "get",
    url: "/ysfwpt/selectManufacturer",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.list.length;i ++){
            $("#manufacturer_list ul").append("<li class='list' title=" + data.list[i].manufacturer + ">" + data.list[i].manufacturer + "</li>");
        }
    }
});

// 设置下拉框宽度（输入框id1，下拉框id2）
var setWidth = function (id1,id2) {
    var width = $("#" + id1).css('width');
    $("#" + id2).css({
        "width": width
    });
};
// 点击搜索框
$("#manufacturer").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#manufacturer_list").is(':visible')){
        $("#manufacturer_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("manufacturer","manufacturer_list div");
        $("#manufacturer_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#manufacturer_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#manufacturer").val(this.innerHTML);
                $("#manufacturer_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#pageSize").click(function (e){
    e.stopPropagation(); //阻止冒泡 
    if($("#pageSize_list").is(':visible')){
        $("#pageSize_list").slideUp(300);
    }
    else{
        $("#pageSize_list").slideDown(300);
        var obj_lis = $("#pageSize_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                pageSize = this.className;
                pageNo = 1;
                $("#pageSize").val(this.innerHTML);
                $("#pageSize_list").slideUp(300);
                refresh();
            }
        }
    }
});

// 搜索框点击任意选项后进行搜索
var object;
$(document).click(function(e) {
    object = $(e.target).attr("class");
    if(object == "list" || object == "state state_chose"){
        // 提交数据自动进行搜索
        refresh();
    }
});

// 点击×清空输入框，并重新搜索刷新页面
$(".icon_clear").click(function () {
    $(this).parent().find('input').val("");
    refresh();
});

//点击搜索
$("#search_button").click(function (){
    searchword = $("#search_input").val();
    refresh();
});

// 点击上一页/下一页
$("#pre").click(function (){
    if(pageNo == 1){
        console.log("首页")
    }
    else{
        pageNo = parseInt(pageNo) - parseInt(1);
        refresh();
    }
});
$("#next").click(function (){
    if(pageNo == pageCount){
        console.log("尾页")
    }
    else{
        pageNo = parseInt(pageNo) + parseInt(1);
        refresh();
    }
});

// 页码跳转输入框失去焦点后跳转页面
$("#pageNo").blur( function(){
    // console.log(pageNo);
    // 页码发生改变
    if(pageNo != $(this).val()){
        // 页码小于最小页码
        if($(this).val() < 1){
            pageNo = 1;
        }
        // 页码大于最大页码
        else if($(this).val() > pageCount){
            pageNo = pageCount;
        }
        else{
            pageNo = $(this).val();
        }
        refresh();
    }
});

var refresh = function () {
    var hospital = $("#hospital").val();
    var manufacturer = $("#manufacturer").val();
    var searchword = $("#search_input").val();
    //                            用户名            医院             生产厂商             查询词           页码          数据数目
    window.location.href = "?" + username + "&" + hospital + "&" + manufacturer + "&" + searchword + "&" + pageNo + "&" + pageSize;
};